<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	</head>
<style type="text/css">
	body {
		margin: 0px;
		padding: 0px;
		font-family: '微软雅黑', '宋体';
	}

	div {
/*		height: 180px;*/
		width: 300px;
	}

	ul {
		margin: 0px;
		padding: 0px;
	}

	li {
/*		margin-left: 5px;*/
		margin-bottom: 5px;
		padding-left: 5px;
		padding-top: 4px;
		height: 25px;
		cursor: pointer;
		border: 1px #83A90D solid;
		list-style-type: none;
		background-color: #BFDF90;
		font-size:	15px;
/*		font-family: '微软雅黑', '宋体';*/
		color: #29282C;
	}

	.tip {
		margin-left: 15px;
		margin-right: 15px;
		float: right;
	}
	
	#videoTip > span {
		font-size: 10px;
		display: none;
	}
</style>

<fieldset>
	<legend>Notifier</legend>
<div>
<ul>
	<li url="/{uid}/atme">
		<span>@我的</span>
		<span id="atme" class="tip">0</span>
	</li>
	<li url="/{uid}/follows">
		<span>关注</span>
		<span id="attention" class="tip">0</span>
	</li>
	<li url="/{uid}/comment">
		<span>回复我的</span>
		<span id="comment" class="tip">0</span>
	</li>
	<li url="/home">
		<span>feed</span>
		<span id="feed" class="tip">0</span>
	</li>
	<li url="/{uid}/msg">
		<span>私信</span>
		<span id="message" class="tip">0</span>
	</li>
</ul>
</div>
</fieldset>
<div style="margin-top: 10px;"></div>
<fieldset>
	<legend>Video Transference</legend>

	<input type="button" value="一键转载哦,亲" id="videoDirect"/>
	<input type="button" value="说两句再转载哦,亲" id="videoIndirect"/>
	
	<div id="videoTip">
		<span id="successfulTip">
			视频转载成功了哦,亲 <a href="http://t.qiyi.com/home" target="_blank">去看看</a>
		</span>
		<span id="failureTip">
			哦吃～失败了哦,亲 再试试
		</span>
		<span id="notSupport">
			这是什么网站，藕们不支持。
		</span>
		<span id="noYet">
			你求我，我就开发介个功能。
		</span>
	</div>
</fieldset>


<script type="text/javascript" src="jquery-1.6.1.min.js"></script>

<script type="text/javascript">
$(document).ready(init);

var uid = 0;
var _currentPageUrl = '';


function log(data) {
	$('#videoTip').text(data);
}


function dealCookie(cookie) {
	uid = cookie.value;
}

function getCurrentPageUrl(url) {
	_currentPageUrl = url
}


function init() {
	chrome.extension.getBackgroundPage().getPageInfo(getCurrentPageUrl);
	$('#videoDirect').click(dealVideo);	
	$('#videoIndirect').click(function() {
		$('#videoTip > span').hide();
		$('#noYet').show();	
	});
	var backgroundData = chrome.extension.getBackgroundPage();

	chrome.cookies.get({url:'http://qiyi.com', name:'P00003'}, dealCookie);

	$('#atme').text(backgroundData.atmeNum);
	$('#attention').text(backgroundData.attentionNum);
	$('#comment').text(backgroundData.commentNum);
	$('#feed').text(backgroundData.feedNum);
	$('#message').text(backgroundData.messageNum);

	$('ul > li').hover(
		function() {
			$(this).css('background-color', '#E3FAD1');
		},

		function() {
			$(this).css('background-color', '#BFDF90');
		}
	);

	$('ul > li').click(function() {
		var url = $(this).attr('url');

		var id = $(this).find(".tip").attr('id');

		url = 'http://t.qiyi.com' + url.replace(/{uid}/ig, uid) + '?_new=' + backgroundData[id + 'Num'];

		backgroundData[id + 'Num'] = 0;

		var data = {
			code : 'A00000',
			data : {
				atme : backgroundData.atmeNum,
				comment : backgroundData.commentNum,
				feed : backgroundData.feedNum,
				attention : backgroundData.attentionNum,
				msg : backgroundData.messageNum
			}
		}
		chrome.extension.getBackgroundPage().newMessageCallback(data);

		window.open(url);

	});
}


function dealVideo() {
	var supportedVideoSites = [/http:\/\/v.youku.com\/v_show\/[0-9a-zA-Z_]+/];

	var supportedVideoSiteNum = supportedVideoSites.length;
	
	for (var i = 0; i < supportedVideoSiteNum; i++) {
		if (_currentPageUrl.search(supportedVideoSites[i]) == -1) {
			$('#videoTip > span').hide();
			$('#notSupport').show();
			
			return ;	
		}
	}
	
	var videoServiceUrl = 'http://t.qiyi.com/api/video/add.php';	

	var postData = {
		feedShortUrl : _currentPageUrl		
	};
	
	$.post(videoServiceUrl, postData, videoCallback, 'json');	
	
}

function videoCallback(data) {
	if (data.code != 'A00000') {
		return ;	
	}	

	var addFeedUrl = 'http://t.qiyi.com/api/feed/add.php';
	
	var postData = {
		'feedCnt'	: data.data,
		'pageId'	: 'home',
		'userId'	: uid 
	};

	$.post(addFeedUrl, postData, feedCallback, 'json');
}

function feedCallback(data) {
	$('#videoTip > span').hide();
	if (data.code == 'A00000') {
		$('#successfulTip').show();
	} else {
		$('#failureTip').show();
		log(data.code);
	}	
}

	</script>
</html>


